export const CubeMap = {
	0: "U",
	1: "U'",
	2: "R",
	3: "R'",
	4: "F",
	5: "F'",
	6: "D",
	7: "D'",
	8: "L",
	9: "L'",
	10: "B",
	11: "B'",
}

export function mergeMove(moves, ignoreDirection = false) {
	const newMoves = [];
	let lastMove = "";
	let lastMoveCount = 0;
	for (let i = 0; i < moves.length; i++) {
		const move = moves[i];
		if (lastMove === move && lastMoveCount < 2) {
			lastMoveCount = lastMoveCount + 1;
		} else {
			if (lastMoveCount > 0) {
				if (lastMoveCount === 1) {
					newMoves.push(lastMove);
				} else if (lastMoveCount === 2) {
					if (ignoreDirection) {
						lastMove = lastMove.replace("'", '');
					}
					newMoves.push(lastMove + "2");
				} else {
					newMoves.push(lastMove + lastMoveCount);
				}
			}
			lastMove = move;
			lastMoveCount = 1;
		}
	}
	if (lastMoveCount > 0) {
		if (lastMoveCount === 1) {
			newMoves.push(lastMove);
		} else if (lastMoveCount === 2) {
			newMoves.push(lastMove + "2");
		} else {
			newMoves.push(lastMove + lastMoveCount);
		}
	}
	return newMoves.join(" ");
}

export const getReverseName = (name) => {
	let rname = name.replace("'", '')
	return /'/g.test(name) ? rname : (name + "'")
}

export const formatDate = (cellValue) => {
	if (cellValue == null || cellValue == "")
		return "";
	var date = new Date(cellValue);
	var year = date.getFullYear();
	var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
	var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
	var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
	var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
	var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
	var ms = date.getMilliseconds();
	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${ms}`;
}

/*
	方差s^2=[(x1-x)^2 +...(xn-x)^2]/n
*/
export const GetVariance = (list) => {
	const m = list.length;
	let sum = 0;
	for (let i = 0; i < list.length; i++) {
		sum = sum + list[i];
	}
	const dAvg = sum / m;
	let dVar = 0;
	for (let i = 0; i < list.length; i++) {
		dVar = dVar + (list[i] - dAvg) * (list[i] - dAvg);
	}
	return dVar / m;
};

export const GetStandardDeviation = (list) => {
	return Math.sqrt(GetVariance(list));
};

export const GetFluency = (list) => {
	const intervalList = [];
	let rotInterval = 0;
	let idx = 0;
	for (let i = 0; i < list.length; i++) {
		const v = list[i];
		if (v.sMove) {
			idx = idx + 1;
			let realInterval = v.iInterval;
			if (idx === 1) {
				realInterval = 0;
			}
			if (rotInterval !== 0) {
				realInterval = v.iInterval + rotInterval;
				rotInterval = 0;
			}
			intervalList.push(realInterval / 1000);
		}
	}
	if (intervalList.length > 2) {
		return Math.floor(100 * Math.exp(-1.11571775657105 * GetStandardDeviation(intervalList)));
	}
	return 0;
};

export const formatTime = (milliseconds) => {
	const totalSeconds = Math.floor(milliseconds / 1000);
	const minutes = Math.floor(totalSeconds / 60)
		.toString()
		.padStart(2, "0");
	const seconds = (totalSeconds % 60).toString().padStart(2, "0");
	const millisecondsFormatted = (milliseconds % 1000).toString().padStart(3, "0");
	return `${minutes}:${seconds}.${millisecondsFormatted}`;
};